import re
import ssl

import requests
from bs4 import BeautifulSoup

if __name__ == "__main__":
    ssl._create_default_https_context = ssl._create_unverified_context
    target = 'https://unsplash.com/t/wallpapers'
    req = requests.get(url=target, verify=False)
    html = req.text
    bf = BeautifulSoup(html, features="html.parser")
    giFan = bf.find_all('div', class_='GIFah')
    gIFahBF = BeautifulSoup(str(giFan[0]), features="html.parser")
    gIFahBF = gIFahBF.find_all("div", class_="MorZF")
    morZF = BeautifulSoup(str(gIFahBF), features="html.parser")
    img = morZF.find('img')
    imgUrl = img.get("src")
    imgUrl = re.sub("&[wq].*", "", imgUrl)
    print(imgUrl)
    params = {"ixlib": "rb-4.0.3", "q": 85, "fm": "jpg", "crop": "entropy", "cs": "srgb", "dl": "1.jpg"}
    req = requests.get(imgUrl, params=params, verify=False)
    print(req.url)
    with open(r"C:/Users/e-Xingfu.Yang/Desktop/Tmp/spider/1.jpg", "wb") as f:
        f.write(req.content)
    print(req)
